<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 模拟tab产生空格
 * @param int $step
 * @param string $string
 * @param int $size
 * @return string
 */
function tab($step = 1, $string = ' ', $size = 4)
{
    return str_repeat($string, $size * $step);
}


/**
 * @param $money   金额
 * @param bool $type  默认元转分 ;  false  分转元
 */

function fmoney($money, $type = true){
    $result = 0;
    $price = 0;
    if($type){
        $price = $money * 100;    // 元转分
        $result = bcadd($price,0);

    }else{

        $st=substr($money, -2);
        $price = $money / 100;    // 分转元
        if($st>0){
            $result = empty($price) ? '0' : sprintf("%01.2f", $price);
        }else{
            $result = empty($price) ? '0' : $price;
        }
    }

    return $result;
}



/**
 * 生成随机数
 * @param number $length 长度
 * @return Ambigous <string>
 */
function random($pfix='', $length = 8) {
    $result = $pfix;
    $chars = 'abcdefghijklmnopqrstuvwxyz1234567890';
    for($p = 0; $p < $length; $p++) {
        $result .= ($p % 2) ? $chars [mt_rand ( 19, 23 )] : $chars [mt_rand ( 0, 18 )];
    }
    return $result;
}



/**
 *
 * 推荐 根据浏览记录 和收藏商品推荐10个商品
 */
function getCommend($uid,$rows=10){

    $coll_cate = array();
    $brow_cate = array();
    $collection = think\Db::table('kyf_shoucang')->where('uid='.$uid)->select();
    $avg_price_c = 0;
    $vag_price_b = 0;
    $coll_num = 0;
    $brow_num = 0;
    $coll_total = 0;
    $brow_total = 0;
    foreach ($collection as $val){
        $goods = think\Db::table('kyf_goods')->where('goods_id='.$val['gid'])->find();
        array_push($coll_cate,$goods['cate_id']);
        $coll_total += $val['goods_price'];
        $coll_num++;
    }
    $avg_price_c = ceil($coll_total/$coll_num);


    $browse = think\Db::table('kyf_liulan')->where('uid='.$uid)->select();
    foreach ($browse as $val){
        $goods = think\Db::table('kyf_goods')->where('goods_id='.$val['gid'])->find();
        array_push($brow_cate,$goods['cate_id']);
        $brow_total += $val['goods_price'];
        $brow_num++;
    }
    $avg_price_b = ceil($brow_total/$brow_num);

    // 平均价格
    $avg_price = ceil(($avg_price_c+$avg_price_b)/2);
    $min_price = $avg_price-ceil($avg_price/2);
    $max_price = $avg_price+ceil($avg_price/2);
    $cart_arr = array_merge($coll_cate,$brow_cate);

    $maps['goods_price'] = array('>=',$min_price);
    $maps['goods_price'] = array('<=',$max_price);
    $maps['cate_id'] = array('in',implode(',',$cart_arr));

    $list = think\Db::table('kyf_goods')->where($maps)->limit($rows)->select();
    if(!empty($list)){
        return $list;
    } else {
        // 没有收藏 没有浏览记录按 销量推荐
        $list = think\Db::table('kyf_goods')->where($maps)->order('sale_num desc')->limit($rows)->select();
        return $list;
    }
}
